home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / graphics / mpega_library / developer / demo / mpega_demo.c < prev    next >
C/C++ Source or Header  |  1999-06-15  |  10KB  |  342 lines

  1. /*------------------------------------------------------------------------------
  2.  
  3.     File    :   MPEGA_demo.c
  4.  
  5.     Author  :   Stéphane TAVENARD
  6.  
  7.     $VER:   MPEGA_demo.c 1.2  (05/06/1998)
  8.  
  9.     (C) Copyright 1997-1998 Stéphane TAVENARD
  10.         All Rights Reserved
  11.  
  12.     #Rev|   Date   |                      Comment
  13.     ----|----------|--------------------------------------------------------
  14.     0   |25/10/1997| Initial revision                                     ST
  15.     1   |02/05/1998| Added some time features                             ST
  16.     2   |05/06/1998| Added standard access option                         ST
  17.  
  18.     ------------------------------------------------------------------------
  19.  
  20.     Demo of how to use MPEGA library
  21.     Use of private bitstream access functions (access to ram buffer)
  22.  
  23. ------------------------------------------------------------------------------*/
  24.  
  25. #include <exec/exec.h>
  26. #include <clib/exec_protos.h>
  27. #include <pragmas/exec_pragmas.h>
  28. #include <dos.h>
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <time.h>
  34.  
  35. #include <libraries/mpega.h>
  36. #include <clib/mpega_protos.h>
  37. #include <pragmas/mpega_pragmas.h>
  38.  
  39. static char *Version = "$VER:MPEGA_demo 1.2 (05.06.98) (C)1997-1998 Stéphane TAVENARD";
  40.  
  41. struct Library *MPEGABase = NULL;
  42.  
  43. char *mpega_name = "mpega.library";
  44.  
  45. MPEGA_STREAM *mps = NULL;
  46.  
  47. // Ram buffer
  48. #define MPEGA_BUFFER_SIZE  (256*1024) // in bytes
  49.  
  50. BYTE   *mpega_buffer = NULL;
  51. ULONG  mpega_buffer_offset = 0;
  52. ULONG  mpega_buffer_size = 0;
  53.  
  54. static int break_cleanup( void )
  55. {
  56.    if( mps ) {
  57.       MPEGA_close( mps );
  58.       mps = NULL;
  59.    }
  60.    if( MPEGABase ) {
  61.       CloseLibrary( MPEGABase );
  62.       MPEGABase = NULL;
  63.    }
  64.    return 1;
  65. }
  66.  
  67. static void exit_cleanup( void )
  68. {
  69.    (void)break_cleanup();
  70. }
  71.  
  72. // Here start our own bitstream access routines
  73.  
  74. static ULONG __saveds __asm def_baccess( register __a0 struct Hook  *hook,
  75.                                          register __a2 APTR          handle,
  76.                                          register __a1 MPEGA_ACCESS *access ) {
  77. /*----------------------------------------------------------------------------
  78. */
  79.  
  80.    switch( access->func ) {
  81.  
  82.       case MPEGA_BSFUNC_OPEN:
  83.          // We don't really need stream_name
  84.          // buffer_size indicate the following read access size
  85.  
  86. printf( "bitstream open: filename='%s'\n", access->data.open.stream_name );
  87. printf( "bitstream open: buffer_size=%d\n", access->data.open.buffer_size );
  88.  
  89.          // Some open errors...
  90.          if( !mpega_buffer ) return NULL;
  91.  
  92.          // initialize some variables
  93.          mpega_buffer_offset = 0;
  94.  
  95.          // We know total size, we can set it
  96.          access->data.open.stream_size = mpega_buffer_size;
  97.  
  98.          // Just return a dummy handle (not NULL)
  99.          return 1;
  100.  
  101.       case MPEGA_BSFUNC_CLOSE:
  102.          if( handle ) {
  103.             // Clean up
  104. printf( "bitstream close\n" );
  105.          }
  106.          break;
  107.       case MPEGA_BSFUNC_READ: {
  108.          LONG read_size;
  109.  
  110.          if( !handle ) return 0; // Check valid handle
  111.  
  112.          read_size = mpega_buffer_size - mpega_buffer_offset;
  113.          if( read_size > access->data.read.num_bytes ) read_size = access->data.read.num_bytes;
  114.  
  115.          if( read_size > 0 ) {
  116.             if( !access->data.read.buffer ) return 0;
  117.             // Fill buffer with our MPEG audio data
  118.             memcpy( access->data.read.buffer, &mpega_buffer[ mpega_buffer_offset ], read_size );
  119.             mpega_buffer_offset += read_size;
  120.          }
  121.          else {
  122.             read_size = 0; // End of stream
  123.          }
  124. //printf( "bitstream read: requested %d bytes, read %d\n", access->data.read.num_bytes, read_size );
  125.  
  126.          return (ULONG)read_size;
  127.       }
  128.       case MPEGA_BSFUNC_SEEK:
  129.          if( !handle ) return 0;
  130.  
  131. printf( "bitstream seek: pos = %d\n", access->data.seek.abs_byte_seek_pos );
  132.          if( access->data.seek.abs_byte_seek_pos <= 0 ) mpega_buffer_offset = 0;
  133.          else if( access->data.seek.abs_byte_seek_pos >= mpega_buffer_size ) return 1;
  134.          else mpega_buffer_offset = access->data.seek.abs_byte_seek_pos;
  135.          return 0;
  136.    }
  137.    return 0;
  138. }
  139.  
  140. static struct Hook def_bsaccess_hook = {
  141.    { NULL, NULL }, def_baccess, NULL, NULL
  142. };
  143.  
  144. int output_pcm( WORD channels, WORD *pcm[ 2 ], LONG count, FILE *out_file )
  145. /*---------------------------------------------------------------------------
  146.    Ouput the current decoded PCM to a file
  147.    Return 0 if Ok
  148. */
  149. {
  150. #define PCM_BUFFER_SIZE (MPEGA_MAX_CHANNELS*MPEGA_PCM_SIZE)
  151.    static WORD *pcm_buffer = NULL;
  152.    if( !out_file ) return -1;
  153.  
  154.    if( !pcm_buffer ) {
  155.       pcm_buffer = (WORD *)malloc( PCM_BUFFER_SIZE * sizeof(WORD) );
  156.       if( !pcm_buffer ) return -1;
  157.    }
  158.    if( channels == 2 ) {
  159.       register WORD *pcm0, *pcm1, *pcmLR;
  160.       register LONG i;
  161.  
  162.       pcm0 = pcm[ 0 ];
  163.       pcm1 = pcm[ 1 ];
  164.       pcmLR = pcm_buffer;
  165.       i = count;
  166.       while( i-- ) {
  167.          *pcmLR++ = *pcm0++;
  168.          *pcmLR++ = *pcm1++;
  169.       }
  170.       fwrite( pcm_buffer, 4, count, out_file );
  171.    }
  172.    else {
  173.       fwrite( pcm[ 0 ], 2, count, out_file );
  174.    }
  175.  
  176.    return 0;
  177.  
  178. } /* output_pcm */
  179.  
  180. int main( int argc, char **argv )
  181. {
  182.    char *in_filename;
  183.    FILE *in_file;
  184.    int frame = 0;
  185.    char *out_filename = NULL;
  186.    FILE *out_file = NULL;
  187.    WORD i;
  188.    LONG pcm_count, total_pcm = 0;
  189.    LONG index;
  190.    WORD *pcm[ MPEGA_MAX_CHANNELS ];
  191.    clock_t clk; // #1
  192.    double secs; // #1
  193.    int custom = 1; // #2
  194. //ULONG ms;
  195.  
  196.    static const char *layer_name[] = { "?", "I", "II", "III" };
  197.    static const char *mode_name[] = { "stereo", "j-stereo", "dual", "mono" };
  198.  
  199.    MPEGA_CTRL mpa_ctrl = {
  200.       NULL,    // Bitstream access is default file I/O
  201.       // Layers I & II settings (mono, stereo)
  202.       { FALSE, { 1, 2, 48000 }, { 1, 2, 48000 } },
  203.       // Layer III settings (mono, stereo)
  204.       { FALSE, { 1, 2, 48000 }, { 1, 2, 48000 } },
  205.       0,           // Don't check mpeg validity at start (needed for mux stream)
  206.       32678        // Stream Buffer size
  207.    };
  208.  
  209.  
  210. //printf( "MPEGA_demo: sizeof( MPEGA_CTRL ) = %d\n", sizeof( MPEGA_CTRL ) );
  211. //printf( "MPEGA_demo: sizeof( MPEGA_STREAM ) = %d\n", sizeof( MPEGA_STREAM ) );
  212.  
  213.    onbreak( break_cleanup );
  214.    atexit( exit_cleanup );
  215.  
  216.    if( argc <= 1 ) {
  217.       fprintf( stderr, "%s\n", &Version[ 5 ] );
  218.       fprintf( stderr, "Usage %s <input mpeg audio file> [<output pcm file>] [-d]\n", argv[ 0 ] );
  219.       fprintf( stderr, "option: -d = default file access (custom bitstream access otherwise)\n" ); // #2
  220.       fprintf( stderr, "This is a demo of how to use mpega.library\n" );
  221.       fprintf( stderr, "This also show how to use custom bitstream access\n" );
  222.       exit( 0 );
  223.    }
  224.  
  225.    MPEGABase = OpenLibrary( mpega_name, 0L );
  226.    if( !MPEGABase ) {
  227.       printf( "Unable to open '%s'\n", mpega_name );
  228.       exit( 0 );
  229.    }
  230.  
  231.    in_filename = argv[ 1 ];
  232.  
  233.    for( i=2; i<argc; i++ ) { // #2
  234.       if( strcmp( argv[ i ], "-d" ) == 0 ) {
  235.          custom = 0;
  236.       }
  237.       else {
  238.          out_filename = argv[ i ];
  239.       }
  240.    }
  241.  
  242.    for( i=0; i<MPEGA_MAX_CHANNELS; i++ ) {
  243.       pcm[ i ] = malloc( MPEGA_PCM_SIZE * sizeof( WORD ) );
  244.       if( !pcm[ i ] ) {
  245.          fprintf( stderr, "Can't allocate PCM buffers\n" );
  246.          exit( 0 );
  247.       }
  248.    }
  249.  
  250.    // Open the output file
  251.    if( out_filename ) {
  252.       out_file = fopen( out_filename, "wb" );
  253.       if( !out_file ) {
  254.          fprintf( stderr, "Can't create output file '%s'\n", out_filename );
  255.          exit( 0 );
  256.       }
  257.    }
  258.  
  259.    if( custom ) { // #2 (custom bitstream access)
  260.       printf( "Custom bitstream access used\n" ); // #2
  261.       mpega_buffer = (BYTE *)malloc( MPEGA_BUFFER_SIZE );
  262.       if( !mpega_buffer ) {
  263.          fprintf( stderr, "Can't allocate MPEG buffer\n" );
  264.          exit( 0 );
  265.       }
  266.  
  267.  
  268.       // Load the stream into a ram buffer
  269.       in_file = fopen( in_filename, "rb" );
  270.       if( !in_file ) {
  271.          fprintf( stderr, "Unable to open file '%s'\n", in_filename );
  272.          exit( 0 );
  273.       }
  274.       mpega_buffer_size = fread( mpega_buffer, 1, MPEGA_BUFFER_SIZE, in_file );
  275.       fclose( in_file );
  276.       printf( "Read %d bytes from file '%s'\n", mpega_buffer_size, in_filename );
  277.  
  278.       // #1 Begin
  279.       printf( "Test if MPEG Audio sync inside...\n" );
  280.       index = MPEGA_find_sync( mpega_buffer, MPEGA_BUFFER_SIZE );
  281.       if( index >= 0 ) {
  282.          printf( "Ok, found MPEG Audio sync at position %d\n", index );
  283.       }
  284.       else {
  285.          printf( "* Error %d *\n", index );
  286.       }
  287.       // #1 End
  288.  
  289.       // Set our bitstream access routines and open the stream
  290.       mpa_ctrl.bs_access = &def_bsaccess_hook;
  291.    }
  292.    else {
  293.       printf( "Default bitstream access used\n" ); // #2
  294. printf( "Buffer size = %d\n", mpa_ctrl.stream_buffer_size );
  295.    }
  296.  
  297.    mps = MPEGA_open( in_filename, &mpa_ctrl );
  298.    if( !mps ) {
  299.       printf( "Unable to find MPEG Audio stream in file '%s'\n", in_filename );
  300.       exit( 0 );
  301.    }
  302.  
  303.    printf( "\n" );
  304.    printf( "MPEG norm %d Layer %s\n", mps->norm, layer_name[ mps->layer ] );
  305.    printf( "Bitrate: %d kbps\n", mps->bitrate );
  306.    printf( "Frequency: %d Hz\n", mps->frequency );
  307.    printf( "Mode: %d (%s)\n", mps->mode, mode_name[ mps->mode ] );
  308.    printf( "Stream duration: %ld ms\n", mps->ms_duration );
  309.    printf( "\n" );
  310.    printf( "Output decoding parameters\n" );
  311.    printf( "Channels: %d\n", mps->dec_channels );
  312.    printf( "Quality: %d\n", mps->dec_quality );
  313.    printf( "Frequency: %d Hz\n", mps->dec_frequency );
  314.    printf( "\n" );
  315.  
  316.    clk = clock(); // #1
  317.    while( (pcm_count = MPEGA_decode_frame( mps, pcm )) >= 0 ) {
  318. //MPEGA_time( mps, &ms );
  319.       total_pcm += pcm_count;
  320.       if( out_file ) output_pcm( mps->dec_channels, pcm, pcm_count, out_file );
  321.       frame++;
  322.       if( (frame & 31) == 0 ) fprintf( stderr, "{%04d}\r", frame ); fflush( stderr );
  323.    }
  324.    clk = clock() - clk; // #1
  325.    secs = (double)clk / (double)CLK_TCK; // #1
  326.    printf( "time used = %7.3f secs\n", secs ); // #1
  327.    printf( "%ld samples / sec\n", (int)((double)total_pcm / secs) );
  328.    printf( "%7.3f % CPU used on real time\n", ((double)mps->frequency * 100) / ((double)total_pcm / secs) );
  329.  
  330.    fprintf( stderr, "\n" );
  331.  
  332.    fprintf( stderr, "last pcm_count = %d\n", pcm_count );
  333.    fprintf( stderr, "total_pcm = %d\n", total_pcm );
  334.  
  335.    MPEGA_close( mps );
  336.    mps = NULL;
  337.    CloseLibrary( MPEGABase );
  338.    MPEGABase = NULL;
  339.  
  340. }
  341.  
  342.